package com.lai.hospital.controller;

import cn.hutool.json.JSONObject;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponseBody;
import com.lai.hospital.annotation.OptLog;
import com.lai.hospital.constant.OptTypeConst;
import com.lai.hospital.dto.UserPatientDTO;
import com.lai.hospital.entity.Patient;
import com.lai.hospital.service.PatientService;
import com.lai.hospital.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/**
 * @author : lai
 * @version V1.0
 * @Project: hospital-springboot
 * @Package com.lai.hospital.controller
 * @Description: 患者控制器
 * @date Date : 2022年11月27日 23:48
 */
@RestController
@Api(tags = "患者模块", value = "患者模块")
public class PatientController {

    @Autowired
    private PatientService patientService;


    @GetMapping("/admin/listPatient")
    @ApiOperation(value = "获取患者列表")
    public Result<PageResult<Patient>> listPatient(ConditionVO conditionVO){
        return Result.ok(patientService.listPatient(conditionVO));
    }

    @DeleteMapping("/admin/user/deletePatient/{id}")
    @ApiOperation(value = "删除患者信息")
    @ApiImplicitParam(name = "id",value = "患者id",required = true,dataType = "Integer")
    @OptLog(optType = OptTypeConst.REMOVE)
    public Result<?> deletePatient(@PathVariable("id") Integer id){
        patientService.deletePatient(id);
        return Result.ok();
    }

    @GetMapping("/admin/user/patient/{id}")
    @ApiOperation(value = "根据id获取患者信息")
    @ApiImplicitParam(name = "id",value = "患者id",required = true,dataType = "Integer")
    public Result<Patient> getPatientById(@PathVariable("id") Integer id){
        return Result.ok(patientService.getPatientById(id));
    }

    @PutMapping("/admin/user/updatePatient")
    @ApiOperation(value = "修改患者所有信息")
    @OptLog(optType = OptTypeConst.UPDATE)
    public Result<?> saveOrUpdatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }

    @GetMapping("/doctor/listMyPatient")
    @ApiOperation(value = "我的患者")
    public Result<PageResult<Patient>> listMyPatient(ConditionVO conditionVO){
        return Result.ok(patientService.listMyPatient(conditionVO));
    }


    @PostMapping("/user/saveOrUpdatePatient")
    @ApiOperation(value = "新增或修改患者基本信息")
    @OptLog(optType = OptTypeConst.SAVE_OR_UPDATE)
    public Result<?> saveOrUpdatePatient(@RequestBody PatientBaseInfoVO patientBaseInfoVO){
        patientService.saveOrUpdatePatient(patientBaseInfoVO);
        return Result.ok();
    }

    @GetMapping("/user/listPatient/{userId}")
    @ApiOperation(value = "根据用户id获取全部就诊人列表")
    @ApiImplicitParam(name = "userId",value = "患者id",required = true,dataType = "Integer")
    public Result<List<UserPatientDTO>> listPatient(@PathVariable("userId") Integer userId){
        return Result.ok(patientService.listPatientByUserId(userId));
    }

    @DeleteMapping("/admin/user/deleteUserPatient/{userPatientId}")
    @ApiOperation(value = "根据用户就诊人id删除用户就诊人")
    @ApiImplicitParam(name = "userPatientId",value = "用户就诊人id",required = true,dataType = "Integer")
    @OptLog(optType = OptTypeConst.REMOVE)
    public Result<?> deleteUserPatient(@PathVariable("userPatientId") Integer userPatientId){
        patientService.deleteUserPatient(userPatientId);
        return Result.ok();
    }

    @PutMapping("/user/updateUserPatientRelation")
    @ApiOperation(value = "修改用户就诊人关系")
    public Result<?> updateUserPatientRelation(@RequestBody UserPatientVO userPatientVO){
        patientService.updateUserPatientRelation(userPatientVO);
        return Result.ok();
    }

    /**
     * 获取用户的默认就诊人信息
     * @return 默认就诊人信息
     */
    @GetMapping("/user/patient/default")
    @ApiOperation(value = "获取用户的默认就诊人信息")
    public Result<Patient> getUserDefaultPatient(){
        return Result.ok(patientService.getUserDefaultPatient());
    }

    @PostMapping("/user/patient/OCR")
    @ApiImplicitParam(name = "file", value = "患者身份证", required = true, dataType = "MultipartFile")
    @ApiOperation(value = "使用OCR识别患者身份证")
    public Result<RecognizeIdcardResponseBody> OCR(MultipartFile file) throws Exception {
        return Result.ok(patientService.OCR(file));
    }





}
